package com.xydtech.ecard.ecard.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.string.StringUtil;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.exception.ApiException;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.service.IEcardLoanCalculatorService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <dd>描述: ~ 电子名片-贷款额度测算Controller</dd>
 * <dd>创建时间：  13:35 2024/1/18</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2024/1/18     yuzhiguo         1.0        1.0 Version
 */
@Controller
@Slf4j
@Api(tags = "电子名片-贷款额度测算接口")
@RequestMapping("/ecardSaasApi/loanCalculator")
public class EcardLoanCalculatorController extends EcardApiBaseController{

    @Autowired
    private ISysWechatAppletService wechatAppletService;

    @Autowired
    private IEcardLoanCalculatorService ecardLoanCalculatorService;

    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 贷款额度计算
     * @param json
     * @return
     */
    @PostMapping(value = "/loanLimitCalculation")
    @ApiOperation(value = "电子名片-贷款额度测算接口", notes = "电子名片-贷款额度测算接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "merchantType", value = "商户类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "familyAssets", value = "家庭资产", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "familyLiabilities", value = "家庭负债", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "monthlyIncome", value = "月收入", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "personalAge", value = "年龄", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isSocialSecurity", value = "是否缴纳社保，0：是，1：否", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAccumulationFund", value = "是否缴纳公积金，0：是，1：否", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "monthlyRevenue", value = "月营业额", dataType = "String", paramType = "query")})
    @ResponseBody
    public ResponseModel loanLimitCalculation(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            if (StringUtil.objIsNullOrEmpty(json)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            JSONObject jsonObject = JSONObject.parseObject(json);
            //根据appId获取deptId
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            String appId = wechatApplet.getAppid();
            if (StringUtils.isEmpty(appId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
            }
            Long deptId = wechatApplet.getDeptId();
            Double finalLimit = ecardLoanCalculatorService.loanLimitCalculation(jsonObject, deptId);//最终额度
            System.out.println("最终额度 = " + finalLimit);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.ok(finalLimit);
        }catch (ApiException ex) {
            return ResponseModel.error(ex);
        } catch (Exception ex) {
            return ResponseModel.error(ex, this.getClass(), "LoanCalculatorController->loanLimitCalculation");
        }
    }
}
